package com.bp.hearthealth.pulse.lnapps.ads.topon.rewarded_ad.manual_load

import android.content.Context
import com.anythink.core.api.ATAdInfo
import com.anythink.core.api.ATNetworkConfirmInfo
import com.anythink.core.api.AdError
import com.anythink.rewardvideo.api.ATRewardVideoExListener
import com.bp.hearthealth.pulse.lnapps.BpApplication
import com.bp.hearthealth.pulse.lnapps.ads.model.AdsCount
import com.bp.hearthealth.pulse.lnapps.ads.topon.interstitial_ad.TopOnInterAdManager
import com.bp.hearthealth.pulse.lnapps.ads.topon.rewarded_ad.TopOnRewardVideoAdManager
import com.bp.hearthealth.pulse.lnapps.bp_extension.LogUtil
import java.util.Timer
import java.util.TimerTask

class ATRewardVideoExListenerImpl : ATRewardVideoExListener {

    private var onRefresh: Boolean = true
    private var adManualLoadListener: TopOnRewardVideoAdListener? = null

    fun setManualLoadListener(topOnRewardVideoAdListener: TopOnRewardVideoAdListener?) {
        this.adManualLoadListener = topOnRewardVideoAdListener
    }

    override fun onDeeplinkCallback(adInfo: ATAdInfo, isSuccess: Boolean) {
        adManualLoadListener?.onDeeplinkCallback(adInfo, isSuccess)
        LogUtil.iTopOn(
            "TopOn 【激励视频广告】 onDeeplinkCallback:$adInfo--status:$isSuccess"
        )
    }

    override fun onDownloadConfirm(
        context: Context?, adInfo: ATAdInfo, networkConfirmInfo: ATNetworkConfirmInfo?
    ) {
        adManualLoadListener?.onDownloadConfirm(context, adInfo, networkConfirmInfo)
        LogUtil.iTopOn("TopOn 【激励视频广告】 onDownloadConfirm: $adInfo")
    }

    //-------------------------- Only for CSJ --------------------------

    /**
     * 再看一个广告的开始播放回调
     */
    override fun onRewardedVideoAdAgainPlayStart(entity: ATAdInfo) {
        adManualLoadListener?.onRewardedVideoAdAgainPlayStart(entity)
        LogUtil.iTopOn(
            "TopOn 【激励视频广告】 onRewardedVideoAdAgainPlayStart:\n$entity"
        )
    }

    /**
     * 再看一个广告的播放结束回调
     */
    override fun onRewardedVideoAdAgainPlayEnd(entity: ATAdInfo) {
        adManualLoadListener?.onRewardedVideoAdAgainPlayEnd(entity)
        LogUtil.iTopOn(
            "TopOn 【激励视频广告】 onRewardedVideoAdAgainPlayEnd:\n$entity"
        )
    }

    /**
     * 再看一个广告的播放失败回调
     */
    override fun onRewardedVideoAdAgainPlayFailed(errorCode: AdError, entity: ATAdInfo?) {
        adManualLoadListener?.onRewardedVideoAdAgainPlayFailed(errorCode, entity)
        LogUtil.iTopOn(
            "TopOn 【激励视频广告】 onRewardedVideoAdAgainPlayFailed error: " + errorCode.fullErrorInfo
        )
    }

    /**
     * 再看一个广告的点击回调
     */
    override fun onRewardedVideoAdAgainPlayClicked(entity: ATAdInfo) {
        adManualLoadListener?.onRewardedVideoAdAgainPlayClicked(entity)
        LogUtil.iTopOn(
            "TopOn 【激励视频广告】 onRewardedVideoAdAgainPlayClicked: $entity"
        )
    }

    /**
     * 再看一个广告的下发激励回调
     */
    override fun onAgainReward(entity: ATAdInfo) {
        adManualLoadListener?.onAgainReward(entity)
        LogUtil.iTopOn("TopOn 【激励视频广告】 onAgainReward:\n$entity")
    }

    //-------------------------- Only for CSJ --------------------------
    /**
     * 广告加载成功回调
     */
    override fun onRewardedVideoAdLoaded() {
        onRefresh = true
        adManualLoadListener?.onRewardedVideoAdLoaded()
        LogUtil.iTopOn("TopOn 【激励视频广告】 onRewardedVideoAdLoaded")
    }

    /**
     * 广告加载失败回调
     */
    override fun onRewardedVideoAdFailed(errorCode: AdError) {
        adManualLoadListener?.onRewardedVideoAdFailed(errorCode)
        if (onRefresh) {
            onRefresh = false
            //延迟10秒加载
            Timer().schedule(object : TimerTask() {
                override fun run() {
                    TopOnRewardVideoAdManager.manualLoadAd()
                    LogUtil.iTopOn("TopOn 【激励视频广告】加载失败 延迟10s后重新请求插页广告。。。。。。。。")
                }
            }, 10000)
        }
        LogUtil.iTopOn(
            "TopOn 【激励视频广告】 onRewardedVideoAdFailed error:" + errorCode.fullErrorInfo
        )
    }

    /**
     * 广告开始播放回调
     */
    override fun onRewardedVideoAdPlayStart(entity: ATAdInfo) {
        BpApplication.adIsShow = true
        AdsCount.incrementDisplayCount()
        adManualLoadListener?.onRewardedVideoAdPlayStart(entity)
        LogUtil.iTopOn(
            "TopOn 【激励视频广告】 onRewardedVideoAdPlayStart:\n$entity"
        )
    }

    /**
     * 广告播放结束回调
     */
    override fun onRewardedVideoAdPlayEnd(entity: ATAdInfo) {
        adManualLoadListener?.onRewardedVideoAdPlayEnd(entity)
        LogUtil.iTopOn(
            "TopOn 【激励视频广告】 onRewardedVideoAdPlayEnd:\n$entity"
        )
    }

    /**
     * 广告播放失败回调
     */
    override fun onRewardedVideoAdPlayFailed(errorCode: AdError, entity: ATAdInfo) {
        adManualLoadListener?.onRewardedVideoAdPlayFailed(errorCode, entity)
        LogUtil.iTopOn(
            "TopOn 【激励视频广告】 onRewardedVideoAdPlayFailed:\n$entity"
        )
    }

    /**
     * 广告关闭回调
     */
    override fun onRewardedVideoAdClosed(entity: ATAdInfo) {
        BpApplication.adIsShow = false
        adManualLoadListener?.onRewardedVideoAdClosed(entity)
        LogUtil.iTopOn(
            "TopOn 【激励视频广告】 onRewardedVideoAdClosed:\n$entity"
        )
    }

    /**
     * 广告点击
     */
    override fun onRewardedVideoAdPlayClicked(entity: ATAdInfo) {
        adManualLoadListener?.onRewardedVideoAdPlayClicked(entity)
        LogUtil.iTopOn(
            "TopOn 【激励视频广告】 onRewardedVideoAdPlayClicked:\n$entity"
        )
    }

    /**
     * 建议在此回调中下发奖励，一般在onRewardedVideoAdClosed之前回调
     */
    override fun onReward(entity: ATAdInfo) {
        adManualLoadListener?.onReward(entity)
        LogUtil.iTopOn("TopOn 【激励视频广告】 onReward:\n$entity")
    }
}